<?php
namespace soft\tagLib;
use think\template\TagLib;
use soft\Controls;

class control extends TagLib{
	
	//自定义控件
    protected $tags = array(
        'search' => array(
        	'attr' => 'id', 'attr' => 'xml', 'close' =>0
		),
        'grid' => array(
	        'attr' => 'id', 'attr' => 'xml', 'attr' => 'padding', 'attr' => 'height',
	        'attr' => 'style', 'attr' => 'class','close' =>0
	    ),
	    'form' => array(
	        'attr' => 'id', 'attr' => 'xml','attr' => 'style', 'attr' => 'class','close' =>0
	    )
 	);
 	
	//搜索控件
 	public function tagSearch ($attr){
        $id = $attr['id'];
        $xml= empty($attr['xml']) ? '' : $attr['xml'];
        $opt = Controls::instance($xml,$id)->getSearchConfig(false); 
        $config= json_encode($opt,JSON_UNESCAPED_UNICODE);
        $yesFn =  $opt["yesFn"];
        if(strpos($yesFn,"(")){
        	$yesFn = substr($yesFn,0,strpos($yesFn,"("));
        }else if(!$yesFn){
        	$yesFn="function(e){}";
        }
        return "
	        <div id='$id'></div>
	        <script type='text/javascript'>
				$(function(){ $id = layui.search.render($config); $id.query($yesFn); });
			</script>
        ";
    }

   //grid控件
   public function tagGrid ($attr){
   		$id = $attr['id'];
        $xml = empty($attr['xml']) ? "" : $attr['xml'];
        $padding = empty($attr['padding']) ? "" : $attr['padding'];
        $height = empty($attr['height']) ? "" : $attr['height'];
        $style = empty($attr['style']) ? "" : $attr['style'];
        $class = empty($attr['class']) ? "" : $attr['class'];
        $opt = Controls::instance($xml,$id)->getGridConfig(false);
		$config = json_encode($opt,JSON_UNESCAPED_UNICODE);
		$script = "<div id='$id' padding='$padding'  height='$height' style='$style' class='$class'></div>";
		$script .= "<script type='text/javascript' id='__xwGridScript'>";
		$script .= "$('#'+'$id').data('data',$config);";
		$script .= "$('#'+'$id').data('xml','$xml');";
		$script .= "$('#__xwGridScript').remove();";
		$script .= "$(function(){ window['$id'] = layui.grid.render('$id','$xml'); });";
		$script .= "</script>";
		return  $script;
    }
   
    //form控件
    public function tagForm ($attr){
        $id = $attr['id']; $xml = $attr['xml'];  $style = $attr['style'];  $class = $attr['class'];
        $opt = Controls::instance($xml,$id)->getFormConfig(false);
		$config = urldecode(json_encode($opt,JSON_UNESCAPED_UNICODE));
		$variable = "";
		foreach(request()->get() as $key => $val){
			$val = urldecode($val);
			$key = urldecode($key);
			$variable  = $variable."window.__$key='$val';";
		}
		$script = "<div id='$id' padding='$padding'  height='$height' style='$style' class='$class'></div>";
		$script .= "<script type='text/javascript' id='__xwFormScript'>";
		$script .= "$('#'+'$id').data('data',$config);";
		$script .= "$('#'+'$id').data('data',$config);";
		$script .= "$('#'+'$id').data('xml','$xml');";
		$script .= "$variable";
		$script .= "$('#__xwFormScript').remove();";
		$script .= "$(function(){ window['$id'] = layui.forms.render('$id','$xml'); });";
		$script .= "</script>";
		return  $script;
    }
   
}
?>